'''
Created on 2011-3-28

@author: Chine
'''
import urllib, httplib

from ChineKingBlog.weibopy.utils import import_simplejson

def get_short_url(url):
    conn = httplib.HTTPConnection('goo.gl')   
    params = urllib.urlencode({'url': url, 'security_token': 'AIzaSyD2CyYgsRzvz_xjW7SUlgwxQ_PFy4Y2UJM'})   
    conn.request('POST', '/api/shorten', headers={"Content-Type":"application/x-www-form-urlencoded"}, body=params)   
    result = conn.getresponse()   
    resultContent = result.read()   
    conn.close()
    
    json = import_simplejson()
    return json.loads(resultContent).get('short_url', None)

if __name__ == "__main__":
    print get_short_url("qinxuye.me");